home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / mui / mui-tools / multiuser / src / support / who.c < prev   
C/C++ Source or Header  |  1995-03-09  |  5KB  |  190 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * Get a list of users who are logged in                            *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993-1994 Geert Uytterhoeven                        *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include    <exec/memory.h>
  12. #include <exec/execbase.h>
  13. #include <proto/exec.h>
  14. #include <proto/dos.h>
  15. #include <string.h>
  16. #include <libraries/multiuser.h>
  17. #include <proto/multiuser.h>
  18.  
  19. #include "Who_rev.h"
  20.  
  21. #include "Locale.h"
  22.  
  23. char __VersTag__[] = VERSTAG;
  24.  
  25.  
  26. static BOOL FillUsers(UWORD users[], ULONG maxusers, ULONG *numusers,
  27.                              struct ExecBase *SysBase, struct muBase *muBase);
  28. static BOOL AddUser(ULONG user, UWORD users[], ULONG maxusers,
  29.                           ULONG *numusers);
  30. static ULONG DumpUsers(UWORD users[], ULONG numusers, BOOL quick,
  31.                                struct DosLibrary *DOSBase, struct muBase *muBase,
  32.                        struct LocaleInfo *li, struct ExecBase *SysBase);
  33.  
  34.  
  35. int __saveds Start(char *arg)
  36. {
  37.     struct ExecBase *SysBase;
  38.     struct DosLibrary *DOSBase;
  39.     struct muBase *muBase = NULL;
  40.     struct RDArgs *args;
  41.     LONG argarray[] = {
  42. #define argAM        0
  43. #define argI        1
  44. #define argQUICK    2
  45.         NULL, NULL, NULL
  46.     };
  47.     UWORD *users;
  48.     ULONG maxusers = 0, numusers;
  49.     LONG error = NULL;
  50.     int rc = RETURN_OK;
  51.     UWORD uid;
  52.     struct LocaleInfo li;
  53.  
  54.     SysBase = *(struct ExecBase **)4;
  55.     
  56.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  57.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  58.         rc = ERROR_INVALID_RESIDENT_LIBRARY;
  59.         goto Exit;
  60.     }
  61.  
  62.     OpenLoc(&li);
  63.  
  64.     args = ReadArgs("AM/S,I/S,Q=QUICK/S", argarray, NULL);
  65.     if (!args)
  66.         error = IoErr();
  67.     else if ((argarray[argAM] && !argarray[argI]) ||
  68.                 (!argarray[argAM] && argarray[argI]))
  69.         PutStr(GetLocS(&li,MSG_INVALID_OPTIONS));
  70.     else if (argarray[argAM]) {
  71.         uid = muGetTaskOwner(NULL)>>16;
  72.         error = DumpUsers(&uid, 1, (BOOL)argarray[argQUICK], DOSBase, muBase, &li, SysBase);
  73.     } else do {
  74.         maxusers += 256;
  75.         if (users = AllocVec(maxusers*sizeof(UWORD), MEMF_CLEAR)) {
  76.             if (FillUsers(users, maxusers, &numusers, SysBase, muBase))
  77.                 error = DumpUsers(users, numusers, (BOOL)argarray[argQUICK], DOSBase,
  78.                                         muBase, &li, SysBase);
  79.             else
  80.                 rc = RETURN_ERROR;
  81.             FreeVec(users);
  82.         } else
  83.             error = IoErr();
  84.     } while (!error && (rc != RETURN_OK));
  85.     FreeArgs(args);
  86. Fail:
  87.     if (error) {
  88.         PrintFault(error, NULL);
  89.         rc = RETURN_ERROR;
  90.     }
  91.  
  92.     CloseLoc(&li);
  93.  
  94. Exit:
  95.     CloseLibrary((struct Library *)muBase);
  96.     CloseLibrary((struct Library *)DOSBase);
  97.  
  98.     return(rc);
  99. }    
  100.  
  101.  
  102.     /*
  103.      *        Fill in the uids
  104.      */
  105.  
  106. static BOOL FillUsers(UWORD users[], ULONG maxusers, ULONG *numusers,
  107.                              struct ExecBase *SysBase, struct muBase *muBase)
  108. {
  109.     BOOL rc;
  110.     struct Task *task;
  111.  
  112.     *numusers = 0;
  113.     Disable();
  114.     rc = AddUser(muGetTaskOwner(SysBase->ThisTask), users, maxusers, numusers);
  115.     for (task = (struct Task *)SysBase->TaskReady.lh_Head;
  116.           (task->tc_Node.ln_Succ) && rc;
  117.           task = (struct Task *)task->tc_Node.ln_Succ)
  118.         rc = AddUser(muGetTaskOwner(task), users, maxusers, numusers);
  119.     for (task = (struct Task *)SysBase->TaskWait.lh_Head;
  120.           (task->tc_Node.ln_Succ) && rc;
  121.           task = (struct Task *)task->tc_Node.ln_Succ)
  122.         rc = AddUser(muGetTaskOwner(task), users, maxusers, numusers);
  123.     Enable();
  124.     return(rc);
  125. }
  126.  
  127.  
  128. static BOOL AddUser(ULONG user, UWORD users[], ULONG maxusers, ULONG *numusers)
  129. {
  130.     ULONG i;
  131.     UWORD uid;
  132.     BOOL found = FALSE;
  133.  
  134.     if (user) {
  135.         uid = user>>16;
  136.         for (i = 0; !found && (i < *numusers); i++)
  137.             found = (users[i] == uid);
  138.         if (found)
  139.             return(TRUE);
  140.         else if (*numusers >= maxusers)
  141.             return(FALSE);
  142.         users[(*numusers)++] = uid;
  143.     }
  144.     return(TRUE);
  145. }
  146.  
  147.  
  148.     /*
  149.      *        Dump the information
  150.      */
  151.  
  152. static ULONG DumpUsers(UWORD users[], ULONG numusers, BOOL quick,
  153.                               struct DosLibrary *DOSBase, struct muBase *muBase,
  154.                        struct LocaleInfo *li, struct ExecBase *SysBase)
  155. {
  156.     struct muUserInfo *info;
  157.     ULONG error = NULL;
  158.     ULONG i;
  159.     LONG stream[2];
  160.  
  161.     if (info = muAllocUserInfo()) {
  162.          for (i= 0; (i < numusers) && !CheckSignal(SIGBREAKF_CTRL_C); i++)
  163.             if (users[i]) {
  164.                 info->uid = users[i];
  165.                 if (muGetUserInfo(info, muKeyType_uid)) 
  166.                     if (quick) {
  167.                         stream[0] = (LONG)info->UserID;
  168.                         VPrintf("%s\n", stream);
  169.                     } else {
  170.                         stream[0] = (LONG)info->UserName;
  171.                         stream[1] = (LONG)info->UserID;
  172.                         VPrintf("%s (%s)\n", stream);
  173.                     }
  174.                 else {
  175.                     stream[0] = info->uid;
  176.                     VPrintf(GetLocS(li,MSG_UNKNOWN_UID), stream);
  177.                 }
  178.             } else if (quick)
  179.                 PutStr("nobody\n"); 
  180.             else {
  181.                 stream[0] = (LONG)GetLocS(li,MSG_NOBODY);
  182.                 VPrintf("%s (nobody)\n", stream);
  183.             }
  184.         muFreeUserInfo(info);
  185.     } else
  186.         error = IoErr();
  187.  
  188.     return(error);
  189. }
  190.